function HandleMailInformation(gui, operator, now, options) {
  this._gui = gui;
  this._operator = operator;
  this._now = now;
  this._options = options;
  
  this._identifiers = (options && options.p) ? options.p : [];
  this._branch = (options && options.b) ? options.b : '';
  this._template = (options && options.t) ? options.t : '';
  
  this._popupBox = new PopupBox(); 
  
  this._createElements();
};

HandleMailInformation.prototype._createElements = function() {
  var pos = [window.screen.width / 2, window.screen.height / 5];
  MiscUtils.load(this._popupBox, pos, 'It may take several seconds. Please wait...');

  this._loadData();
};

HandleMailInformation.prototype._verifyData = function() {
  if (this._produces && this._couriers && this._lastProduce) {
    this._updateElements();
  }
};

HandleMailInformation.prototype._loadData = function() {
  this._produces = null;
  this._couriers = null;
  this._lastProduce = null;
  this._retrieveProduces();
  this._retrieveCouriers();
  this._retrieveLastProduce();
};

HandleMailInformation.prototype._retrieveProduces = function() {
  var _self = this;
  var joins = '&selects=0' + '&purchaseitems=0' + '&preconusers=0' + '&events=0' + '&tracks=0' + '&produces=0' + '&notes=0';
  var args = '&c=' + DlmanageUtils.enquiry(this._identifiers) + joins;
  new RequestUtils()._mysql('produce', args, function(result, params) { _self._produces = result.data;
                                                                        _self._verifyData.call(_self);
                                                                      }, { php: Constant.FACTORY + '/customphp/produceread.php'});
};

HandleMailInformation.prototype._retrieveLastProduce = function() {
  var _self = this;
  var joins = '&selects=0' + '&purchaseitems=0' + '&preconusers=0' + '&events=0' + '&tracks=0' + '&produces=0' + '&notes=0';
  var args = '&c=d.branch=\'' + this._branch + '\'' + ' AND ' + 'd.pi_model = \'{{$smarty.const.MODEL_NAME_MAIL|escape:javascript}}\'' + '&p=1' + '&s=1' + '&q=DESC' + '&o=d.e_oid' + joins;
  new RequestUtils()._mysql('produce', args, function(result, params) { _self._lastProduce = (result.data.length == 1) ? result.data[0] : Produce.instance();
                                                                        _self._verifyData.call(_self);
                                                                      }, { php: Constant.FACTORY + '/customphp/produceread.php'});
};

HandleMailInformation.prototype._retrieveCouriers = function() {
  var _self = this;
  new RequestUtils()._read('icourier', null, '', null, null, null, null, function(result, params) { _self._couriers = result.data;
                                                                                                    _self._verifyData.call(_self);
                                                                                                  }, { php: Constant.FACTORY + '/jsonread.php' });
};

HandleMailInformation.prototype._updateElements = function() {
  var _self = this;
  DlmanageUtils.clear(this._gui);
  this._popupBox._close();

  var branch = (this._produces.length > 0) ? this._produces[0].branch : '';
  var host = (this._produces.length > 0) ? this._produces[0].host : '';
  
  
  this._gui.company.options[this._gui.company.options.length] = new Option('');
  for (var i = 0, il = this._couriers.length; i < il; i++) {
    var courier = this._couriers[i];
    var option = new Option(courier.name);
    this._gui.company.options[this._gui.company.options.length] = option;
    if (option.text == this._lastProduce.company) {
      this._gui.company.selectedIndex = this._gui.company.options.length - 1;
    }
  }
  
  this._gui.person.value = this._lastProduce.receiver;
  this._gui.address.value = this._lastProduce.address;
  this._gui.mailNo.value = this._lastProduce.code;
  this._gui.weight.value = this._lastProduce.weight;
  this._gui.comment.value = this._lastProduce.comment;
  /*---------------zjx----------------*/
  this._gui.person.onchange = function() { _self._lastProduce.receiver = this.value; };
  this._gui.address.onchange = function() { _self._lastProduce.address = this.value; };
  this._gui.mailNo.onchange = function() { _self._lastProduce.code = this.value; };
  this._gui.weight.onchange = function() { _self._lastProduce.weight = this.value; };
  this._gui.comment.onchange = function() { _self._lastProduce.comment = this.value; };
  /*---------------------------------*/
  this._gui.add.onclick = function() { var object = ICourier.instance();
                                      var div = document.createElement('div');
                                      var func1 = function() { _self._popupBox._close();
                                                               _self._lastProduce.company = object.name;
                                                               new RequestUtils()._write('icourier', [object], [], function() { _self._retrieveCouriers.call(_self); }, { pos: DOMUtils.findPos(_self._gui.add) });
                                                             };
                                      var func2 = function() { _self._popupBox._close();
                                                             };
                                      MiscUtils.dialog(_self._popupBox, DOMUtils.findPos(this), div, func1, func2, { ok: 'Add' });
                                      new DialogIObject(div, object, { name: 'icourier', title: 'Couriers' });
                                    };
  
  this._gui.send.onclick = function() { var str = '';
                                        if (_self._gui.person.value == '') {
                                          str = str + '收件人,';
                                        } 
                                        if(_self._gui.address.value == '') {
                                          str = str + '地址,';
                                        }
                                        if(_self._gui.mailNo.value == '') {
                                          str = str + '邮包编号,';
                                        }
                                        if (_self._gui.company.value == '') {
                                          str = str + '邮寄公司,';
                                        } 
                                        if(_self._gui.weight.value == '') {
                                          str = str + '重量,';
                                        } 
                                        if (_self._gui.person.value == '' || _self._gui.address.value == '' || _self._gui.mailNo.value == '' || _self._gui.company.value == '' || _self._gui.weight.value == '') {
                                          window.alert(str.substring(0, str.length - 1) + ' 未填写');
                                        } else {
                                          var tmp = Produce.instance();
                                          tmp.branch = branch;
                                          tmp.host = host;
                                          tmp.state = 1;
                                          tmp.deadline = SimpleDate.forward(_self._now, 7 * 86400);
                                          tmp.title = _self._gui.mailNo.value;
                                          tmp.receivedDate = null;
                                          tmp.receiver = _self._gui.person.value;
                                          tmp.address = _self._gui.address.value;
                                          tmp.code = _self._gui.mailNo.value;
                                          tmp.company = _self._gui.company.value;
                                          tmp.weight = _self._gui.weight.value;
                                          tmp.comment = _self._gui.comment.value;
                                          tmp.review = '';
                                          tmp.pi_trackId = MiscUtils.unique();
                                          tmp.pi_model = Constant.MODEL_NAME_MAIL;
                                          var object = DlmanageUtils.send(tmp, _self._produces, Manage.map(), _self._operator, _self._now);
                                          var p = object.p;
                                          var e = object.e;
                                          var t = object.t;
                                          p[p.length] = tmp;
                                          new RequestUtils()._write2([{ n: 'produce', a: p, r: [] }, { n: 'produceevent', a: e, r: [] }, { n: 'producetrack', a: t, r: [] }], function() { location.href = '?p=pageserv&t=pageb1videosending&m=' + JSON.stringify({ a: 1, b: 1, c: 1, d: 17}); }, { php: Constant.FACTORY + '/jsonwrite2.php', pos: DOMUtils.findPos(this) });
                                        }
                                      };
  
  this._gui.cancel.onclick = function() { history.back(); };
  new TableProducts(this._gui.products, this._produces, { user: this._operator, date: this._now, callbackFunc: function() { _self._updateElements.call(_self); } });
};